package com.factory.iamp.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.factory.iamp.pojo.*;
import com.factory.iamp.service.TableService;
import com.factory.iamp.utils.helper.CheckTypeCode;
import com.factory.iamp.utils.helper.FileUploadHelper;
import com.factory.iamp.utils.helper.PageHelper;
import com.factory.iamp.utils.helper.ParamsHelper;
import com.factory.iamp.utils.token.LoginToken;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/table")
@Validated
public class TableController {
    @Resource
    private TableService tableService;

    private List<Integer> makeTypeList(int type) {
        List<Integer> result = new ArrayList<>();
        for(int i = 0; type > 0; i++, type /= 2) if(type % 2 == 1) result.add(i);
        return result;
    }

    private String makeEndStr(String end) {
        if(end.length() < 1) {
            LocalDate date = LocalDate.now().plusDays(1);
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            end = date.format(formatter);
        }
        return end;
    }

    @GetMapping("/find/{tid}")
    @LoginToken
    public TableDetail getTableById(
            @PathVariable("tid") int tid,
            @RequestAttribute("iampUser") User user
    ) {
        return tableService.getTableById(tid, user.getLine());
    }

    @GetMapping("/unfinishedproblem/{limit}/{page}")
    @LoginToken
    public PageHelper getUnfinishedProblemTable(
            @PathVariable("limit") int limit,
            @PathVariable("page") int page,
            @RequestAttribute("iampUser") User user
    ) {
        PageHelper result = new PageHelper(tableService.findUnfinishedProblemTableCount(), page, limit);
        result.setData(tableService.findUnfinishedProblemTableListPaged(result, user.getLine()));
        return result;
    }

    @GetMapping("/find/name/{limit}/{page}")
    @LoginToken
    public PageHelper findTableByName(
            @PathVariable("limit") int limit,
            @PathVariable("page") int page,
            @RequestParam("name") String name,
            @RequestAttribute("iampUser") User user
    ) {
        name += "%";
        PageHelper result = new PageHelper(tableService.findByNameCount(name, user.getLine()), page, limit);
        result.setData(tableService.findByNameListPaged(result, name, user.getLine()));
        return result;
    }

    @GetMapping("/find/sellid/{limit}/{page}")
    @LoginToken
    public PageHelper findTableBySellid(
            @PathVariable("limit") int limit,
            @PathVariable("page") int page,
            @RequestParam("sellid") List<Integer> sellid,
            @RequestAttribute("iampUser") User user
    ) {
//        System.out.println(data.getJSONArray("sellid"));
//        List<Integer> sellid = data.getJSONArray("sellid").toJavaList(Integer.class);
        PageHelper result = new PageHelper(tableService.findBySellidCount(sellid, user.getLine()), page, limit);
        result.setData(tableService.findBySellidListPaged(result, sellid, user.getLine()));
        return result;
    }

    @GetMapping("/find/my/{limit}/{page}")
    @LoginToken
    public PageHelper findMyTableAll(
            @PathVariable("limit") int limit,
            @PathVariable("page") int page,
            @RequestParam(value = "name", defaultValue = "") String name,
            @RequestParam(value = "type", required = false) String type,
            @RequestParam(value = "creator", required = false) String creator,
            @RequestParam(value = "handler", required = false) String handler,
            @RequestParam(value = "begin", defaultValue = "1970-01-01") String begin,
            @RequestParam(value = "end", defaultValue = "9999-12-31") String end,
            @RequestParam(value = "order", defaultValue = "2") String order,
            @RequestParam(value = "filter", defaultValue = "-1") int filter,
            @RequestAttribute("iampUser") User user
    ) {
        ParamsHelper paramsHelper = new ParamsHelper(
                name, type, creator, handler, "created_at", begin, end
        );
        String QueryStr = paramsHelper.makeQueryStringOrder(order, "tid");
        PageHelper result = new PageHelper(tableService.findMyCount(user.getUid(), QueryStr, user.getLine(), filter), page, limit);
        result.setData(tableService.findMyListPaged(result, user.getUid(), QueryStr, user.getLine(), filter));
        return result;
    }

    @GetMapping("/find/my/created/{limit}/{page}")
    @LoginToken
    public PageHelper findMyTableByCreator(
            @PathVariable("limit") int limit,
            @PathVariable("page") int page,
            @RequestParam(value = "name", defaultValue = "") String name,
            @RequestParam(value = "type", defaultValue = "(1, 2, 3)") String type,
            @RequestParam(value = "handler", required = false) String handler,
            @RequestParam(value = "begin", defaultValue = "1970-01-01") String begin,
            @RequestParam(value = "end", defaultValue = "9999-12-31") String end,
            @RequestParam(value = "order", defaultValue = "2") String order,
            @RequestParam(value = "filter", defaultValue = "-1") int filter,
            @RequestAttribute("iampUser") User user
    ) {
        ParamsHelper paramsHelper = new ParamsHelper(
                name, type, null, handler, "created_at", begin, end
        );
        String QueryStr = paramsHelper.makeQueryStringOrder(order, "tid");
        PageHelper result = new PageHelper(
                tableService.findByCreatorCount(user.getUid(), QueryStr, user.getLine(), filter), page, limit
        );
        result.setData(tableService.findByCreatorListPaged(result, user.getUid(), QueryStr, user.getLine(), filter));
        return result;
    }

    @GetMapping("/find/my/handling/{limit}/{page}")
    @LoginToken
    public PageHelper findMyTableByHandler(
            @PathVariable("limit") int limit,
            @PathVariable("page") int page,
            @RequestParam(value = "name", defaultValue = "") String name,
            @RequestParam(value = "type", defaultValue = "(1)") String type,
            @RequestParam(value = "creator", required = false) String creator,
            @RequestParam(value = "begin", defaultValue = "1970-01-01") String begin,
            @RequestParam(value = "end", defaultValue = "9999-12-31") String end,
            @RequestParam(value = "order", defaultValue = "2") String order,
            @RequestParam(value = "filter", defaultValue = "-1") int filter,
            @RequestAttribute("iampUser") User user
    ) {
        ParamsHelper paramsHelper = new ParamsHelper(
                name, type, creator, null, "created_at", begin, end
        );
        String QueryStr = paramsHelper.makeQueryStringOrder(order, "tid");
        PageHelper result = new PageHelper(
                tableService.findByHandlerCount(user.getUid(), QueryStr, user.getLine(), filter), page, limit
        );
        result.setData(tableService.findByHandlerListPaged(result, user.getUid(), QueryStr, user.getLine(), filter));
        return result;
    }

    @GetMapping("/find/my/passed/{limit}/{page}")
    @LoginToken
    public PageHelper findMyPassedTable(
            @PathVariable("limit") int limit,
            @PathVariable("page") int page,
            @RequestParam(value = "name", defaultValue = "", required = false) String name,
            @RequestParam(value = "type", defaultValue = "3", required = false) int type,
            @RequestParam(value = "begin", defaultValue = "2022-01-01", required = false) String begin,
            @RequestParam(value = "end", defaultValue = "", required = false) String end,
            @RequestParam(value = "filter", defaultValue = "-1") int filter,
            @RequestAttribute("iampUser") User user
    ) {
        name += "%";
        List<Integer> typeList = makeTypeList(type);
        end = makeEndStr(end);
        PageHelper result = new PageHelper(
                tableService.findMyPassedTableCount(
                        user.getUid(), name, typeList, begin, end, user.getLine(), filter
                ), page, limit
        );
        result.setData(tableService.findMyPassedTableListPaged(
                result, user.getUid(), name, typeList, begin, end, user.getLine(), filter
        ));
        return result;
    }

    @GetMapping("/find/byMainOrder/{tid}/{limit}/{page}")
    @LoginToken
    public PageHelper getSubTable(
            @PathVariable("tid") int tid,
            @PathVariable("limit") int limit,
            @PathVariable("page") int page,
            @RequestAttribute("iampUser") User user
    ) {
        PageHelper result = new PageHelper(tableService.SubTableCount(tid, user.getLine()), page, limit);
        result.setData(tableService.SubTableListPaged(result, tid, user.getLine()));
        return result;
    }

    @GetMapping("/list/{limit}/{page}")
    @LoginToken
    public PageHelper getAllTable(
            @PathVariable("limit") int limit,
            @PathVariable("page") int page,
            @RequestParam(value = "name", defaultValue = "") String name,
            @RequestParam(value = "type", required = false) String type,
            @RequestParam(value = "creator", required = false) String creator,
            @RequestParam(value = "handler", required = false) String handler,
            @RequestParam(value = "begin", defaultValue = "1970-01-01") String begin,
            @RequestParam(value = "order", defaultValue = "2") String order,
            @RequestParam(value = "end", defaultValue = "9999-12-31") String end,
            @RequestAttribute("iampUser") User user
    ) {
        ParamsHelper paramsHelper = new ParamsHelper(
                name, type, creator, handler, "created_at", begin, end
        );
        String QueryStr = paramsHelper.makeQueryStringOrder(order, "tid");
        PageHelper result = new PageHelper(tableService.QueryCount(QueryStr, user.getLine()), page, limit);
        result.setData(tableService.QueryListPaged(result, QueryStr, user.getLine()));
        return result;
    }

    @GetMapping("/problem/{pid}/list/{limit}/{page}")
    @LoginToken
    public PageHelper getTableByProblem(
            @PathVariable("pid") int pid,
            @PathVariable("limit") int limit,
            @PathVariable("page") int page,
            @RequestParam(value = "order", defaultValue = "2") String order,
            @RequestParam(value = "type", defaultValue = "3") int type,
            @RequestParam(value = "filter", defaultValue = "-1") int filter,
            @RequestAttribute("iampUser") User user
    ) {
        String QueryStr = "not ISNULL(sign)";
        if(type == 1) QueryStr = "sign = 'need'";
        else if(type == 2) QueryStr = "sign <> 'need'";
        PageHelper result = new PageHelper(tableService.CountByProblem(pid, QueryStr, order, filter), page, limit);
        result.setData(tableService.ListByProblemPaged(result, pid, QueryStr, order, user.getLine(), filter));
        return result;
    }

    @GetMapping("/store/list/{limit}/{page}")
    @LoginToken
    public PageHelper getAllStoreTable(
            @PathVariable("limit") int limit,
            @PathVariable("page") int page,
            @RequestParam(value = "name", defaultValue = "") String name,
            @RequestParam(value = "type", required = false) String type,
            @RequestParam(value = "creator", required = false) String creator,
            @RequestParam(value = "handler", required = false) String handler,
            @RequestParam(value = "begin", defaultValue = "1970-01-01") String begin,
            @RequestParam(value = "end", defaultValue = "9999-12-31") String end,
            @RequestParam(value = "order", defaultValue = "2") String order,
            @RequestParam(value = "filter", defaultValue = "-1") int filter,
            @RequestAttribute("iampUser") User user
    ) {
        ParamsHelper paramsHelper = new ParamsHelper(
                name, type, creator, handler, "created_at", begin, end
        );
        String QueryStr = paramsHelper.makeQueryString();
        PageHelper result = new PageHelper(tableService.StoreCount(QueryStr, user.getLine(), filter), page, limit);
        result.setData(tableService.StoreListPaged(result, QueryStr, order, user, filter));
        return result;
    }

    @GetMapping("/assembly/list/{limit}/{page}")
    @LoginToken
    public PageHelper getAllAssemblyTable(
            @PathVariable("limit") int limit,
            @PathVariable("page") int page,
            @RequestParam(value = "name", defaultValue = "") String name,
            @RequestParam(value = "begin", defaultValue = "1970-01-01") String begin,
            @RequestParam(value = "end", defaultValue = "9999-12-31") String end,
            @RequestAttribute("iampUser") User user
    ) {
        ParamsHelper paramsHelper = new ParamsHelper(
                name, null, null, null, "created_at", begin, end
        );
        String QueryStr = paramsHelper.makeQueryString();
        PageHelper result = new PageHelper(tableService.findAssemblyCount(QueryStr, user.getLine()), page, limit);
        result.setData(tableService.findAssemblyListPaged(result, QueryStr, user.getLine()));
        return result;
    }

    @GetMapping("/data/inspection")
    @LoginToken
    public List<JSONObject> getDeliveryInspectionData(
            @RequestParam(value = "range", defaultValue = "7") int range,
            @RequestParam(value = "line", defaultValue = "-1") int line
    ) {
        return tableService.getDeliveryInspectionData(range, line);
    }

    @GetMapping("/data/problems")
    @LoginToken
    public Object getProblemRecordData(@RequestAttribute("iampUser") User user) {
        return tableService.getProblemRecordData(user.getLine());
    }

    @GetMapping("/data/check/tables") // 避免tid列表过长无法传递参数
    @LoginToken
    public JSONArray getCheckStateByName(
            @RequestParam("name") String name,
            @RequestParam(value = "begin", defaultValue = "1970-01-01") String begin,
            @RequestParam(value = "end", defaultValue = "9999-12-31") String end,
            @RequestAttribute("iampUser") User user
    ) {
        ParamsHelper paramsHelper = new ParamsHelper(
                null, null, null, null, "created_at", begin, end
        );
        String QueryStr = paramsHelper.makeQueryString();
        return tableService.getCheckStateByName(QueryStr, "%" + name + "%", user.getLine());
    }

    @GetMapping("/{tid}/checktime")
    @LoginToken
    public List<CheckLog> getTableCheckTime(@PathVariable("tid") int tid) {
        return tableService.getTableCheckTime(tid);
    }

    @GetMapping("/{tid}/problemsign")
    @LoginToken
    public List<QuestionLog> getTableProblemSign(@PathVariable("tid") int tid) {
        return tableService.getTableProblemSign(tid);
    }

    @PostMapping("/data/check/tables") // 避免tid列表过长无法传递参数
    @LoginToken
    public JSONArray getCheckStateByTableList(
            @RequestParam("list") String tidList,
            @RequestParam(value = "begin", defaultValue = "1970-01-01") String begin,
            @RequestParam(value = "end", defaultValue = "9999-12-31") String end,
            @RequestAttribute("iampUser") User user
    ) {
        ParamsHelper paramsHelper = new ParamsHelper(
                null, null, null, null, "created_at", begin, end
        );
        paramsHelper.setTidList(tidList);
        String QueryStr = paramsHelper.makeQueryString();
        return tableService.getCheckStateByTableList(QueryStr, user.getLine());
    }

    @PostMapping("/fill/{tid}/{oid}")
    @LoginToken
    public TableContent fillTableContent(
            @RequestBody JSONObject data,
            @PathVariable("tid") int tid,
            @PathVariable("oid") int oid,
            @RequestAttribute("iampUser") User user
    ) {
        return tableService.fillTableContent(tid, oid, user.getUid(), new TableContent(data), user.getLine());
    }

    @PostMapping("/fill/head/{tid}")
    @LoginToken
    public Table fillTableHead(
            @RequestBody JSONObject data,
            @PathVariable("tid") int tid,
            @RequestAttribute("iampUser") User user
    ) {
        return tableService.fillTableHead(
                tid, user.getUid(), data.getJSONObject("content").toJSONString(), user.getLine()
        );
    }

    @GetMapping("/draft/list/{limit}/{page}")
    @LoginToken
    public PageHelper getAllDraftTable(
            @PathVariable("limit") int limit,
            @PathVariable("page") int page,
            @RequestAttribute("iampUser") User user,
            @RequestParam(value = "name", defaultValue = "") String name,
            @RequestParam(value = "begin", defaultValue = "1970-01-01") String begin,
            @RequestParam(value = "end", defaultValue = "9999-12-31") String end
    ) {
        ParamsHelper paramsHelper = new ParamsHelper(
                name, null, null, null, "created_at", begin, end
        );
        String QueryStr = paramsHelper.makeQueryString();
        PageHelper result = new PageHelper(tableService.DraftCount(user.getUid(), QueryStr, user.getLine()), page, limit);
        result.setData(tableService.DraftListPaged(result, user.getUid(), QueryStr, user.getLine()));
        return result;
    }

    @PostMapping("/scheck/{tid}/{oid}")
    @LoginToken
    public TableContent scheckTableContent(
            @PathVariable("tid") int tid,
            @PathVariable("oid") int oid,
            @RequestAttribute("iampUser") User user,
            @RequestParam(value = "file", required = false) MultipartFile file
    ) throws Throwable {
        if(file != null)
            return tableService.checkTableContent(
                    tid, oid, 1, user.getUid(), FileUploadHelper.upload_sign(tid, file), user.getLine()
            );
        return tableService.checkTableContent(
                tid, oid, 1, user.getUid(), user.getSign_img(), user.getLine()
        );
    }

    @PostMapping("/ocheck/{tid}/{oid}")
    @LoginToken
    public TableContent ocheckTableContent(
            @PathVariable("tid") int tid,
            @PathVariable("oid") int oid,
            @RequestAttribute("iampUser") User user,
            @RequestParam(value = "file", required = false) MultipartFile file
    ) throws Throwable {
        if(file != null)
            return tableService.checkTableContent(
                    tid, oid, 2, user.getUid(), FileUploadHelper.upload_sign(tid, file), user.getLine()
            );
        return tableService.checkTableContent(
                tid, oid, 2, user.getUid(), user.getSign_img(), user.getLine()
        );
    }

    @PostMapping("/zcheck/{tid}/{oid}")
    @LoginToken(checkPrivate = true)
    public TableContent zcheckTableContent(
            @PathVariable("tid") int tid,
            @PathVariable("oid") int oid,
            @RequestAttribute("iampUser") User user,
            @RequestParam(value = "file", required = false) MultipartFile file
    ) throws Throwable {
        if(file != null)
            return tableService.checkTableContent(
                    tid, oid, 3, user.getUid(), FileUploadHelper.upload_sign(tid, file), user.getLine()
            );
        return tableService.checkTableContent(
                tid, oid, 3, user.getUid(), user.getSign_img(), user.getLine()
        );
    }

    @PostMapping("/problem/{tid}/{oid}")
    @LoginToken(checkPrivate = true)
    public TableContent addTableContentProblem(
            @PathVariable("tid") int tid,
            @PathVariable("oid") int oid,
            @RequestParam("problem") String problem,
            @RequestAttribute("iampUser") User user,
            @RequestParam(value = "file", required = false) MultipartFile file
    ) throws Throwable {
        if(file != null)
            return tableService.addTableContentProblem(
                    tid, oid, user.getUid(), JSONObject.parseObject(problem), FileUploadHelper.upload_sign(tid, file)
            );
        return tableService.addTableContentProblem(
                tid, oid, user.getUid(), JSONObject.parseObject(problem), user.getSign_img()
        );
    }

    @PostMapping("/problem/process/{tid}/{oid}")
    @LoginToken
    public void processTableContentProblem(
            @PathVariable("tid") int tid,
            @PathVariable("oid") int oid,
            @RequestParam("record") String record,
            @RequestAttribute("iampUser") User user,
            @RequestParam(value = "file", required = false) MultipartFile file
    ) throws Throwable {
        if(file != null)
            tableService.processTableContentProblem(
                    tid, oid, user.getUid(), record, FileUploadHelper.upload_sign(tid, file)
            );
        else tableService.processTableContentProblem(tid, oid, user.getUid(), record, user.getSign_img());
    }

    @PostMapping("/precast/{tid}")
    @LoginToken
    public Table updatePrecastId(
            @PathVariable("tid") int tid,
            @RequestParam("pre_cast_id") String precastid,
            @RequestAttribute("iampUser") User user
    ) {
        return tableService.updatePrecastId(tid, precastid, user.getUid(), user.getLine());
    }

    @PostMapping("/sell/{tid}")
    @LoginToken
    public Table updateSellId(
            @PathVariable("tid") int tid,
            @RequestParam("sell_id") String sellid,
            @RequestAttribute("iampUser") User user
    ) {
        return tableService.updateSellId(tid, sellid, user.getUid(), user.getLine());
    }

    @PostMapping("/pass/{tid}/{uid}")
    @LoginToken
    public Table passTableToUid(
            @PathVariable("tid") int tid,
            @PathVariable("uid") String uid,
            @RequestAttribute("iampUser") User user
    ) {
        return tableService.passTableToUid(tid, uid, user.getUid(), user.getLine());
    }

    @PostMapping("/store/claim/{tid}")
    @LoginToken
    public Table claimTable(
            @PathVariable("tid") int tid,
            @RequestAttribute("iampUser") User user
    ) {
        return tableService.claimTable(tid, user.getUid(), user.getLine());
    }

    @PostMapping("/store/{tid}")
    @LoginToken
    public Table storeTable(
            @PathVariable("tid") int tid,
            @RequestAttribute("iampUser") User user
    ) {
        return tableService.storeTable(tid, user.getLine());
    }

    @PostMapping("/archive/{tid}")
    @LoginToken(checkPrivate = true)
    public Table archiveTable(
            @PathVariable("tid") int tid,
            @RequestAttribute("iampUser") User user
    ) {
        return tableService.archiveTable(tid, user.getLine());
    }

    @PostMapping("/file/{tid}")
    @LoginToken
    public String makeTableFile(
            @PathVariable("tid") int tid,
            @RequestAttribute("iampUser") User user
    ) throws Throwable {
        return tableService.makeTableFile(tid, user.getLine());
    }

    @PostMapping("/assembly/copy/{aid}")
    @LoginToken
    public TableAssembly copyAssemblyTable(
            @RequestParam("name") String name,
            @PathVariable("aid") int aid,
            @RequestAttribute("iampUser") User user
    ) {
        return tableService.copyAssemblyTable(name, aid, user.getLine());
    }

    @PostMapping("/assembly/add")
    @LoginToken
    public JSONObject addAssemblyTable(@RequestBody JSONObject data, @RequestAttribute("iampUser") User user) {
        data.put("creator", user.getUid());
        data.put("lid", user.getLine());
        return tableService.addAssemblyTable(data, user.getLine());
    }

    @PostMapping("/assembly/alter/{aid}")
    @LoginToken
    public TableAssembly alterAssemblyTable(
            @RequestBody JSONObject data,
            @PathVariable("aid") int aid,
            @RequestAttribute("iampUser") User user
    ) {
        return tableService.alterAssemblyTable(aid, data, user.getLine());
    }

    @PostMapping("/alter/{tid}")
    @LoginToken
    public Table alterTable(
            @RequestBody JSONObject data,
            @PathVariable("tid") int tid,
            @RequestAttribute("iampUser") User user
    ) {
        return tableService.alterTable(tid, user.getUid(), data, user.getLine());
    }

    @PostMapping("/multi/add")
    @LoginToken
    public void addMultiTable(@RequestBody JSONObject data, @RequestAttribute("iampUser") User user) {
        Table table = new Table(data);
        table.setCreator(user.getUid());
        table.setLid(user.getLine());
        table.setFtid(-1);
        JSONArray names = data.getJSONArray("nameList"), subTable = data.getJSONArray("subTable");
        for(Object name: names) {
            table.setName((String) name);
            table = tableService.addDraftTable(table, user.getLine());
            tableService.publishDraftTable(table.getTid(), user.getUid(), user.getLine());
            for(Object sub: subTable) {
                Table sTable = new Table(JSON.parseObject(JSON.toJSONString(sub)));
                sTable.setName(table.getName() + sTable.getName());
                sTable.setCreator(user.getUid());
                sTable.setLid(user.getLine());
                sTable = tableService.addDraftTable(sTable, user.getLine());
                tableService.publishDraftTable(sTable.getTid(), user.getUid(), user.getLine());
                tableService.setFatherTable(sTable.getTid(), table.getTid());
            }
        }
    }

    @PostMapping("/draft/add")
    @LoginToken
    public Table addDraftTable(@RequestBody JSONObject data, @RequestAttribute("iampUser") User user) {
        Table table = new Table(data), res = null;
        table.setCreator(user.getUid());
        table.setLid(user.getLine());
        return tableService.addDraftTable(table, user.getLine());
    }

    @PostMapping("/draft/alter/{tid}")
    @LoginToken
    public Table alterDraftTable(
            @RequestBody JSONObject data,
            @PathVariable("tid") int tid,
            @RequestAttribute("iampUser") User user
    ) {
        return tableService.alterDraftTable(tid, user.getUid(), data, user.getLine());
    }

    @PostMapping("/draft/publish/{tid}")
    @LoginToken
    public Table publishDraftTable(
            @PathVariable("tid") int tid,
            @RequestAttribute("iampUser") User user
    ) {
        return tableService.publishDraftTable(tid, user.getUid(), user.getLine());
    }

    @PostMapping("/draft/store/{tid}")
    @LoginToken
    public Table storeDraftTable(
            @PathVariable("tid") int tid,
            @RequestAttribute("iampUser") User user
    ) {
        return tableService.storeDraftTable(tid, user.getUid(), user.getLine());
    }

    @PostMapping("/return/{tid}")
    @LoginToken
    public Table returnFactory(
            @PathVariable("tid") int tid,
            @RequestParam("reason") String reason,
            @RequestParam("process") String process,
            @RequestAttribute("iampUser") User user
    ) {
        return tableService.returnFactory(tid, reason, process, user.getLine());
    }

    @PostMapping("/setto/{tid}")
    @LoginToken
    public void setFatherTable(@PathVariable("tid") int tid, @RequestParam("ftid") int ftid) {
        tableService.setFatherTable(tid, ftid);
    }

    @DeleteMapping("/draft/{tid}")
    @LoginToken
    public void deleteDraftTable(@PathVariable("tid") int tid, @RequestAttribute("iampUser") User user) {
        tableService.deleteDraftTable(tid, user.getUid(), user.getLine());
    }

    @DeleteMapping("/assembly/{aid}")
    @LoginToken
    public void deleteAssemblyTable(@PathVariable("aid") int aid, @RequestAttribute("iampUser") User user) {

        tableService.deleteAssemblyTable(aid, user);
    }

    @DeleteMapping("/{tid}/{oid}/{sign}")
    @LoginToken
    public void deleteTableContentSign(
            @PathVariable("sign") String sign,
            @PathVariable("tid") int tid,
            @PathVariable("oid") int oid,
            @RequestAttribute("iampUser") User user
    ) {
        tableService.deleteTableContentSign(tid, oid, user, CheckTypeCode.NameToCode(sign));
    }

    @DeleteMapping("/question/{tid}/{oid}")
    @LoginToken
    public void deleteTableQuestion(
            @PathVariable("tid") int tid,
            @PathVariable("oid") int oid,
            @RequestAttribute("iampUser") User user
    ) {
        tableService.deleteTableQuestion(tid, oid, user.getUid(), user.getLine());
    }

    @DeleteMapping("/question/sign/{tid}/{oid}")
    @LoginToken
    public void deleteTableQuestionSign(
            @PathVariable("tid") int tid,
            @PathVariable("oid") int oid,
            @RequestAttribute("iampUser") User user
    ) {
        tableService.deleteTableQuestionSign(tid, oid, user.getUid(), user.getLine());
    }

    @DeleteMapping("/problem/sign/{tid}/{oid}")
    @LoginToken
    public void deleteTableProblemSign(
            @PathVariable("tid") int tid,
            @PathVariable("oid") int oid,
            @RequestAttribute("iampUser") User user
    ) {
        tableService.deleteTableProblemSign(tid, oid, user.getUid(), user.getLine());
    }

    @DeleteMapping("/{tid}")
    @LoginToken
    public void deleteTable(
            @PathVariable("tid") int tid,
            @RequestAttribute("iampUser") User user
    ) {
        tableService.deleteTable(tid, user);
    }
}
